import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_order.dart';
import '../../../../utils/log_util.dart';

class OrderManagerCenterController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 0;
  int pageSize = 30;
  List<OrderBean> orderList = [];
  int _tabIndex = 0;
  String _userName;
  String _orderSn;
  bool loadingList = false;

  int allCount = 0;
  int sendCount = 0;
  int finishCount = 0;

  initLonding() {
    _orderSn = null;
    _userName = null;
    _tabIndex = 0;
    currPage = 0;
    requestGoodsList();
  }

  Future<bool> requestGoodsList() async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    map2["tabIndex"] = _tabIndex;
    if (_orderSn != null) {
      map2["orderSn"] = _orderSn;
    }
    if (_userName != null) {
      map2["userName"] = _userName;
    }
    map2["page"] = currPage;
    map2["size"] = pageSize;
    int userId = userBean.userId;

    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.orderManagerListUrl, queryParameters: map2);
    List<OrderBean> themList = [];
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data["page"];
      totalCount = data["totalCount"] ?? 0;
      totalPage = data["totalPage"] ?? 0;
      currPage = data["currPage"] ?? 0;
      pageSize = data["pageSize"] ?? 0;
      dynamic list = data["list"];
      if (list != null) {
        for (dynamic item in list) {
          OrderBean bean = OrderBean.fromMap(item);
          themList.add(bean);
        }
      }

      allCount = responseInfo.data["allCount"] ?? 0;
      sendCount = responseInfo.data["sendCount"] ?? 0;
      finishCount = responseInfo.data["finishCount"] ?? 0;
    }
    if (currPage == 1) {
      orderList = themList;
    } else {
      if (themList.length > 0) {
        orderList.addAll(themList);
      }
    }
    loadingList = false;
    update(["orderList"]);
    return true;
  }

  Future<bool> onPullRefresh({int index = 0}) async {
    currPage = 1;
    await requestGoodsList();
    return true;
  }

  Future<bool> onRefresh({int index = 0}) async {
    _tabIndex = index;
    currPage = 1;
    await requestGoodsList();
    return true;
  }


  TextEditingController _textEditingController = new TextEditingController();
  bool sendLoading = false;

  void confirm(OrderBean item) async {
    _textEditingController.clear();
    dynamic reault = await Get.defaultDialog(
      radius: 4,
      title: "温馨提示",
      content: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            RichText(
              //文字居中
              textAlign: TextAlign.left,
              //文字区域
              text: TextSpan(
                text: "请输入用户",
                style: TextStyle(color: Colors.black, fontSize: 16),
                children: [
                  TextSpan(
                    text: "${item.userName}",
                    style: TextStyle(color: Colors.blue),
                  ),
                  TextSpan(
                    text: "的订单",
                    style: TextStyle(color: Colors.grey),
                  ),
                  TextSpan(
                    text: "${item.orderSn}",
                    style: TextStyle(color: Colors.blue),
                  ),
                  TextSpan(
                    text: "对应的取货码",
                    style: TextStyle(color: Colors.grey),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 10,
            ),
            TextField(
              controller: _textEditingController,
              maxLength: 6,
              decoration: InputDecoration(
                  hintText: "请输入取货码", border: OutlineInputBorder()),
            )
          ],
        ),
      ),
      confirm: Container(
        alignment: Alignment.center,
        child: TextButton(
          onPressed: () {
            String result = _textEditingController.text;
            if (result.length == 0) {
              ToastUtils.showToast("请输入取货码");
              return;
            }
            Get.back(result: true);
          },
          child: Text(
            "确认",
            style: TextStyle(color: Colors.blue),
          ),
        ),
        width: 100,
      ),
      cancel: Container(
        alignment: Alignment.center,
        child: TextButton(
            onPressed: () {
              Get.back(result: false);
            },
            child: Text("取消")),
        width: 100,
      ),
    );

    if (reault == null || !reault) {
      return;
    }
    sendLoading = true;
    update(["sendshop"]);
    String result = _textEditingController.text;
    Map<String, dynamic> map2 = {};
    map2["orderId"] = item.id;
    map2["formatCode"] = result;
    ResponseInfo responseInfo = await DioUtils.instance
        .postRequest(url: HttpHelper.confirmManagerOrderUrl, jsonMap: map2);
    if (responseInfo.success) {
      //解析数据
      ToastUtils.showToast("已确认取货");
    } else {
      ToastUtils.showToast(responseInfo.message);
    }
    update(["sendshop"]);
    sendLoading = false;
    requestGoodsList();
  }

  void onSerach({String userName, String orderSn}) {
    currPage = 1;
    _tabIndex = 0;
    _userName = userName;
    _orderSn = orderSn;
    loadingList = true;
    update(["orderList"]);
    requestGoodsList();
  }

  bool _isLoadingMore = false;

  void loadingMore() {
    if (orderList.length < pageSize) {
      LogUtils.e("不满一页 无须加载更多");
      return;
    }
    if (currPage >= totalPage) {
      LogUtils.e("暂无更多");
      return;
    }
    if (!_isLoadingMore) {
      _isLoadingMore = true;
      currPage++;
      requestGoodsList();
    }
  }
}
